home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / intl / datamgmt / example-1.1 / LinguaPanel.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  10.3 KB  |  304 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18.  
  19. import java.applet.Applet;
  20.  
  21. import java.util.Locale;
  22. import java.util.ResourceBundle;
  23. import java.util.Calendar;
  24. import java.util.Date;
  25. import java.util.TimeZone;
  26.  
  27. import java.text.NumberFormat;
  28. import java.text.DateFormat;
  29. import java.text.MessageFormat;
  30.  
  31. import java.net.URL;
  32.  
  33. class LinguaPanel extends Panel implements Runnable {
  34.  
  35.     private Label localeLabel;
  36.     private Label localeValue;
  37.  
  38.     private Label dateLabel;
  39.     private Label dateValue;
  40.     private Calendar cal;
  41.     private Date date;
  42.     private DateFormat dateFormatter;
  43.  
  44.     private Label timeLabel;
  45.     private Label timeValue;
  46.     private DateFormat timeFormatter;
  47.  
  48.     private Label gdpLabel;
  49.     private Label gdpValue;
  50.     private Double gdp;
  51.     private NumberFormat gdpFormatter;
  52.  
  53.     private Label populationLabel;
  54.     private Label populationValue;
  55.     private Integer population;
  56.     private NumberFormat populationFormatter;
  57.  
  58.     private Label literacyLabel;
  59.     private Label literacyValue;
  60.     private Double literacy;
  61.     private NumberFormat literacyFormatter;
  62.  
  63.     private TextArea description;
  64.  
  65.     private SoundCanvas[] soundCanvases;
  66.     private Panel soundPanel;
  67.  
  68.     private Font defaultFont;
  69.     private Font boldFont;
  70.  
  71.     private Thread clockThread = null;
  72.     private Locale currentLocale;
  73.  
  74.     LinguaPanel(Locale locale, Applet parentApplet,
  75.                 Font defaultFont, Font boldFont, Image[] soundImages, String[] soundNames)
  76.         throws java.util.MissingResourceException {
  77.  
  78.     ResourceBundle labels = ResourceBundle.getBundle("LabelsBundle", locale);
  79.     ResourceBundle paragraph = ResourceBundle.getBundle("ParagraphBundle", locale);
  80.     ResourceBundle numbers = ResourceBundle.getBundle("NumbersBundle", locale);
  81.     ResourceBundle soundBundle = ResourceBundle.getBundle("SoundBundle", locale);
  82.  
  83.     currentLocale = locale;
  84.  
  85.     // create the GUI elements for the locale name display
  86.         localeLabel = new Label();
  87.         localeLabel.setFont(boldFont);
  88.         localeValue = new Label();
  89.  
  90.         add(localeLabel);
  91.         add(localeValue);
  92.  
  93.     // create the GUI elements for the Date display and add them to the window
  94.         dateLabel = new Label();
  95.         dateLabel.setFont(boldFont);
  96.         dateValue = new Label();
  97.  
  98.         add(dateLabel);
  99.         add(dateValue);
  100.  
  101.         timeLabel = new Label();
  102.         timeLabel.setFont(boldFont);
  103.         timeValue = new Label();
  104.  
  105.         add(timeLabel);
  106.         add(timeValue);
  107.  
  108.     // create the GUI elements for the gdp display and add them to the window
  109.         gdpLabel = new Label();
  110.         gdpLabel.setFont(boldFont);
  111.         gdpValue = new Label();
  112.  
  113.         add(gdpLabel);
  114.         add(gdpValue);
  115.  
  116.     // create the GUI elements for the population display and add them to the window
  117.         populationLabel = new Label();
  118.         populationLabel.setFont(boldFont);
  119.         populationValue = new Label();
  120.  
  121.         add(populationLabel);
  122.         add(populationValue);
  123.  
  124.     // create the GUI elements for the literacy display and add them to the window
  125.         literacyLabel = new Label();
  126.         literacyLabel.setFont(boldFont);
  127.         literacyValue = new Label();
  128.  
  129.         add(literacyLabel);
  130.         add(literacyValue);
  131.  
  132.     // create the sound canvases and add them to the window
  133.     soundPanel = new Panel();
  134.     soundPanel.setLayout(new FlowLayout());
  135.     soundCanvases = new SoundCanvas[soundImages.length];
  136.     for (int i = 0; i < soundCanvases.length; i++) {
  137.         soundCanvases[i] = new SoundCanvas(soundImages[i], this,
  138.         soundImages[i].getHeight(this), soundImages[i].getWidth(this),
  139.         soundBundle.getString(soundNames[i]), parentApplet);
  140.         soundPanel.add(soundCanvases[i]);
  141.     }
  142.     add(soundPanel);
  143.  
  144.     // create a text area
  145.     description = new TextArea(paragraph.getString("LocaleDescription"),
  146.         7, 10, TextArea.SCROLLBARS_VERTICAL_ONLY);
  147.     description.setEditable(false);
  148.         add(description);
  149.  
  150.     // set text for GUI items based on locale
  151.         localeLabel.setText(labels.getString("LocaleLabel"));
  152.         localeValue.setText(currentLocale.getDisplayName(currentLocale));
  153.  
  154.     Object[] args = { labels.getString("RepCity") };
  155.     String result = MessageFormat.format(labels.getString("TimeLabel"), args);
  156.         timeLabel.setText(result);
  157.  
  158.     timeFormatter = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale);
  159.     TimeZone tz = TimeZone.getTimeZone(labels.getString("TimeZone"));
  160.     timeFormatter.setTimeZone(tz);
  161.  
  162.     updateTime(currentLocale);
  163.  
  164.         dateLabel.setText(labels.getString("DateLabel"));
  165.     dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale);
  166.     dateFormatter.setTimeZone(tz);
  167.         dateValue.setText(dateFormatter.format(date));
  168.  
  169.         gdpLabel.setText(labels.getString("GDPLabel"));
  170.     gdpFormatter = NumberFormat.getCurrencyInstance(currentLocale);
  171.     gdp = (Double) numbers.getObject("GDP");
  172.         gdpValue.setText(gdpFormatter.format(gdp));
  173.  
  174.         populationLabel.setText(labels.getString("PopulationLabel"));
  175.     populationFormatter = NumberFormat.getNumberInstance(currentLocale);
  176.     population = (Integer) numbers.getObject("Population");
  177.         populationValue.setText(populationFormatter.format(population));
  178.  
  179.         literacyLabel.setText(labels.getString("LiteracyLabel"));
  180.     literacyFormatter = NumberFormat.getPercentInstance(currentLocale);
  181.     literacy = (Double) numbers.getObject("Literacy");
  182.         literacyValue.setText(literacyFormatter.format(literacy));
  183.  
  184.     layoutGUI();
  185.  
  186.     if (clockThread == null) {
  187.             clockThread = new Thread(this, "Clock");
  188.             clockThread.start();
  189.         }
  190.     }
  191.  
  192.     private void updateTime(Locale locale) {
  193.     cal = Calendar.getInstance();
  194.     date = cal.getTime();
  195.         timeValue.setText(timeFormatter.format(date));
  196.     }
  197.  
  198.     private void layoutGUI() {
  199.         GridBagConstraints c = new GridBagConstraints();
  200.         GridBagLayout gridbag = new GridBagLayout();
  201.         setLayout(gridbag);
  202.  
  203.         c.fill = GridBagConstraints.BOTH;
  204.  
  205.     // layout locale stuff
  206.     c.insets = new Insets(5,5,0,5);
  207.         c.anchor = GridBagConstraints.EAST;
  208.     c.fill = GridBagConstraints.VERTICAL;
  209.         c.gridwidth = 1; // reset to the default
  210.         c.weightx = 1.0; // 2 columns of equal width
  211.         gridbag.setConstraints(localeLabel, c);
  212.  
  213.         c.anchor = GridBagConstraints.CENTER; // reset to default
  214.         c.fill = GridBagConstraints.BOTH; //reset to app default
  215.         c.gridwidth = GridBagConstraints.REMAINDER; // end of row
  216.         gridbag.setConstraints(localeValue, c);
  217.  
  218.     // layout time stuff
  219.     c.insets = new Insets(0,5,0,5);
  220.         c.anchor = GridBagConstraints.EAST;
  221.     c.fill = GridBagConstraints.VERTICAL;
  222.         c.gridwidth = 1; // reset to the default
  223.         c.weightx = 1.0; // 2 columns of equal width
  224.         gridbag.setConstraints(timeLabel, c);
  225.  
  226.         c.fill = GridBagConstraints.BOTH; //reset to app default
  227.         c.gridwidth = GridBagConstraints.REMAINDER; // end of row
  228.         gridbag.setConstraints(timeValue, c);
  229.  
  230.     // layout date stuff
  231.     c.insets = new Insets(0,5,0,5);
  232.         c.anchor = GridBagConstraints.EAST;
  233.     c.fill = GridBagConstraints.VERTICAL;
  234.         c.gridwidth = 1; // reset to the default
  235.         c.weightx = 1.0; // 2 columns of equal width
  236.         gridbag.setConstraints(dateLabel, c);
  237.  
  238.         c.anchor = GridBagConstraints.CENTER; // reset to default
  239.         c.fill = GridBagConstraints.BOTH; //reset to app default
  240.         c.gridwidth = GridBagConstraints.REMAINDER; // end of row
  241.         gridbag.setConstraints(dateValue, c);
  242.  
  243.     // layout gdp stuff
  244.         c.anchor = GridBagConstraints.EAST;
  245.     c.fill = GridBagConstraints.VERTICAL;
  246.         c.gridwidth = 1; //reset to the default
  247.     c.weightx = 0.0; //reset to the default
  248.         gridbag.setConstraints(gdpLabel, c);
  249.  
  250.         c.anchor = GridBagConstraints.CENTER; // reset to default
  251.         c.fill = GridBagConstraints.BOTH; //reset to app default
  252.         c.gridwidth = GridBagConstraints.REMAINDER; // end of row
  253.         gridbag.setConstraints(gdpValue, c);
  254.  
  255.     // layout population stuff
  256.         c.anchor = GridBagConstraints.EAST;
  257.     c.fill = GridBagConstraints.VERTICAL;
  258.         c.gridwidth = 1; //reset to the default
  259.         gridbag.setConstraints(populationLabel, c);
  260.  
  261.         c.anchor = GridBagConstraints.CENTER; // reset to default
  262.         c.fill = GridBagConstraints.BOTH; //reset to app default
  263.         c.gridwidth = GridBagConstraints.REMAINDER; // end of row
  264.         gridbag.setConstraints(populationValue, c);
  265.  
  266.     // layout population literacy stuff
  267.         c.anchor = GridBagConstraints.EAST;
  268.     c.fill = GridBagConstraints.VERTICAL;
  269.         c.gridwidth = 1; //reset to the default
  270.         gridbag.setConstraints(literacyLabel, c);
  271.  
  272.         c.anchor = GridBagConstraints.CENTER; // reset to default
  273.         c.fill = GridBagConstraints.BOTH; //reset to app default
  274.         c.gridwidth = GridBagConstraints.REMAINDER; // end of row
  275.         gridbag.setConstraints(literacyValue, c);
  276.  
  277.     // layout image panel
  278.     c.insets = new Insets(5,5,5,5);
  279.         c.fill = GridBagConstraints.BOTH;
  280.         c.gridwidth = GridBagConstraints.REMAINDER; // end of row
  281.         gridbag.setConstraints(soundPanel, c);
  282.  
  283.     // layout text area
  284.         c.fill = GridBagConstraints.BOTH;
  285.         c.gridwidth = GridBagConstraints.REMAINDER; // end of row
  286.         c.weighty = 1.0; // as tall as possible
  287.         gridbag.setConstraints(description, c);
  288.  
  289.     }
  290.  
  291.     // [PENDING: should stop this thread when applet is asked to stop]
  292.     public synchronized void run() {
  293.         // loop terminates when clockThread is set to null in stop()
  294.         while (Thread.currentThread() == clockThread) {
  295.             updateTime(currentLocale);
  296.             try {
  297.                 Thread.sleep(1000);
  298.             } catch (InterruptedException e){
  299.             }
  300.         }
  301.     }
  302.  
  303. }
  304.